﻿using UnityEngine;
using System.Collections;
#if !NAKED
using supersdkUnity;
#endif

public class GameExit : MonoBehaviour
{
    GameObject box;
    void Update()
    {
#if !NAKED
        if (Input.GetKeyUp(KeyCode.Escape))
        {
            if (box == null)
            {
                if (GameConfig.Instance.platform == 2 && supersdkUnity.SuperSDKUnity3D.isSupported(SuperSDKDataKeys.Function.EXIT))
                {

                    //TODO 在Exit回调中判断有无退出对话框
                    if (SuperSDKUnity3D.hasExitDialog())
                    {
                        /**
                          * SDK支持退出操作，必须调用SDK本身的退出方法，使用SDK的退出界面
                          */
                        supersdkUnity.SuperSDKUnity3D.call(SuperSDKDataKeys.Function.EXIT);
                    }
                    else
                    {
                        /**
                           * 使用游戏自身的退出界面退出游戏
                           */
                        string exit_txt = LanguageTool.GetTextNoUpdate(1048);

                        box = GameObject.Instantiate(Resources.Load("UI/MessageBox/OneDelMsgBoxWithClose")) as GameObject;
                        box.transform.parent = BoxManager.Anthor;
                        box.transform.localScale = Vector3.one;
                        box.transform.localPosition = Vector3.zero;

                        box.GetComponent<MessageBoxDelegateCtrl>().Init(exit_txt, delegate()
                        {
                            supersdkUnity.SuperSDKUnity3D.call(SuperSDKDataKeys.Function.EXIT);
                        });
                    }
                }
                else
                {
                    /**
                     * 使用游戏自身的退出界面退出游戏
                     */
                    string exit_txt = LanguageTool.GetTextNoUpdate(1048);

                    box = GameObject.Instantiate(Resources.Load("UI/MessageBox/OneDelMsgBoxWithClose")) as GameObject;
                    box.transform.parent = BoxManager.Anthor;
                    box.transform.localScale = Vector3.one;
                    box.transform.localPosition = Vector3.zero;

                    box.GetComponent<MessageBoxDelegateCtrl>().Init(exit_txt, delegate()
                    {
                        Application.Quit();
                    });

                }
            }
            else
            {
                Destroy(box);
            }
        }
#endif
    }
}
